package cn.cc.goods.service.serviceImpl;

import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.encrypt.EncryptUtil;
import cn.cc.common.result.utils.RestResult;

import cn.cc.common.result.utils.RestResultEmum;
import cn.cc.goods.mapper.mapper.SmallMiGoodsSkuMapper;
import cn.cc.goods.mapper.pojo.SmallMiGoodsSku;
import cn.cc.goods.service.GoodsSkuService;
import cn.cc.goods.service.GoodsSpuService;
import cn.cc.goods.service.bo.AddGoodsSkuBO;
import cn.cc.goods.service.bo.DelGoodsSkuBO;
import cn.cc.goods.service.bo.UpdGoodsSkuBO;
import cn.cc.goods.service.domain.GetGoodsSkuDO;
import cn.cc.goods.service.domain.GetGoodsSpuDO;
import cn.cc.goods.service.domain.GetSkuByGoodsIdDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @program: smail-mi-cloud
 * @description: 商品sku           service
 * @author: Mr.WuChao
 * @create: 2021-07-16 14:32
 **/
@Service
public class GoodsSkuServiceImpl implements GoodsSkuService {
    @Autowired
    private SmallMiGoodsSkuMapper goodsSkuMapper;
    @Autowired
    private GoodsSpuService spuService;

    @Override
    public RestResult<Void> addGoodsSku(AddGoodsSkuBO skBO) {
        SmallMiGoodsSku miGoodsSku = BeanCopierUtil.copyObj(skBO, SmallMiGoodsSku.class);
        RestResult<List<GetGoodsSpuDO>> goodsSpu = spuService.getGoodsSpu();
        //随机生成规格信息
        String[] color = {"白色", "黑色", "棕色"};
        String[] size = {"8+128G", "6+128G", "4+64G"};
        //随机库存
        String[] repertory = {"300", "500", "450", "100"};
        //价格随机
        double[] price = {2900.00, 1799.00, 1899.00, 3199.00};
        for (int i = 0; i < 2; i++) {
            for (GetGoodsSpuDO datum : goodsSpu.getData()) {
                //自动生成id
                long l = System.currentTimeMillis();//时间戳
                String mi = String.valueOf(l);
                EncryptUtil instance = EncryptUtil.getInstance();
                String r = instance.MD5(mi);
                miGoodsSku.setSkuId(r);
                String str1 = color[(int) (Math.random() * color.length)];
                String str2 = size[(int) (Math.random() * size.length)];
                String str3 = repertory[(int) (Math.random() * repertory.length)];
                double v = price[(int) (Math.random() * repertory.length)];
                BigDecimal decimal = new BigDecimal(v);
                miGoodsSku.setGoodsId(datum.getGoodsId());
                miGoodsSku.setGoodsStandard(str1 + str2);
                miGoodsSku.setGoodsPrice(decimal);
                miGoodsSku.setGoodsRepertory(str3);
                miGoodsSku.setCreationDate(new Date());
                miGoodsSku.setGoodsStatus("1");
                miGoodsSku.setVersion("1");
                miGoodsSku.setCreatedBy("cc");
                miGoodsSku.setModifyDate(new Date());
                int rs = goodsSkuMapper.insertSelective(miGoodsSku);
                System.out.println();
            }
        }
        return new RestResult<Void>();
    }


    @Override
    public RestResult<List<GetGoodsSkuDO>> getGoodSkuAll() {
        List<SmallMiGoodsSku> goodsSkus = goodsSkuMapper.selectAll();
        List<GetGoodsSkuDO> skuDOS = BeanCopierUtil.copyList(goodsSkus, GetGoodsSkuDO.class);
        if (skuDOS == null || skuDOS.size() == 0) {
            return new RestResult<>(RestResultEmum.GET_ERROR.code, RestResultEmum.GET_ERROR.msg);
        }
        return new RestResult<>(skuDOS);
    }

    @Override
    public RestResult<Void> delGoodsSku(DelGoodsSkuBO delGoodsSkuBO) {
        SmallMiGoodsSku goodsSkus = BeanCopierUtil.copyObj(delGoodsSkuBO, SmallMiGoodsSku.class);
        int delete = goodsSkuMapper.delete(goodsSkus);
        if (delete == 0) {
            return new RestResult<>(RestResultEmum.DEL_ERROR.code, RestResultEmum.DEL_ERROR.msg);
        }
        return new RestResult<>();
    }

    @Override
    public RestResult<Void> updGoodsSku(UpdGoodsSkuBO updGoodsSkuBO) {
        SmallMiGoodsSku goodsSkus = BeanCopierUtil.copyObj(updGoodsSkuBO, SmallMiGoodsSku.class);
        int update = goodsSkuMapper.updateByPrimaryKeySelective(goodsSkus);
        if (update == 0) {
            return new RestResult<>(RestResultEmum.EDIT_ERROR.code, RestResultEmum.EDIT_ERROR.msg);
        }
        return new RestResult<>();
    }

    @Override
    public RestResult<Void> addGoodSkuOne(AddGoodsSkuBO addGoodsSkuBO) {
        SmallMiGoodsSku goodsSkus = BeanCopierUtil.copyObj(addGoodsSkuBO, SmallMiGoodsSku.class);
        int insertSelective = goodsSkuMapper.insertSelective(goodsSkus);
        if (insertSelective == 0) {
            return new RestResult<>(RestResultEmum.SAVE_ERROR.code, RestResultEmum.SAVE_ERROR.msg);
        }
        return new RestResult<>();
    }
}
